# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=freerdp
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.9.0
pkgrel=2
pkgdesc="Free implementation of the Remote Desktop Protocol (RDP) (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.freerdp.com/"
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-ffmpeg"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libusb"
         "${MINGW_PACKAGE_PREFIX}-openssl")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-iconv")
source=("https://github.com/${_realname}/${_realname}/releases/download/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0001-fix-warnings-mingw.patch"
        "0002-fix-build-errors-mingw.patch"
        "0003-remove-declspec-import-export.patch"
        "0004-remove-conflict-ntml-type.patch"
        "0005-rename-interface-variable.patch"
        "0006-fix-manpage_formatting.patch"
        "0007-read-QuadPart-from-LARGE_INTEGER.patch"
        "0008-fix-build-clang-x64.patch")
sha256sums=('fcf71cf5b09c5c2636341ba212f34b8fb728246ea28e08caf6cef8b4a96184b7'
            '9e71e16b23cb7e1285bc395370b78cc9ca72f897bb8650820d5b4d5e5fcb5176'
            '06d869d249484b57f8da868c8cd5498d96bc4f3bcf669139e2bc7da8bf48a9e6'
            '7b5b85a83af0d2243d334cc4b4cd80e95077ca00c161792cf27cee62c42eae0f'
            '530442612c47a07e538c0f4d803dcfb7050b9b2715d8f42bf02891623383ae6f'
            '5b3fe9a7d4d5db2096b0786c88da30ff8c2819b1703aa995badd8f90550d56e1'
            'f091fcdf13a4693835ca1aa0a3d5ef2f15b97c77f91af5a937e3acfbded14678'
            '9c728341d0e7cd3896cee34b4dce78c18d5235a2cc336e2eae63e5fda354ac08'
            '24e22252f766ea0c0520e12fddcbfb26820dd31200894750938237f52b7c6346')

prepare() {
  mv -v "FreeRDP-${pkgver}" "${_realname}-${pkgver}"
  cd "${_realname}-${pkgver}"

  # https://github.com/FreeRDP/FreeRDP/pull/6983
  patch -Np1 -i "${srcdir}/0001-fix-warnings-mingw.patch"

  # https://github.com/FreeRDP/FreeRDP/pull/7034
  patch -Np1 -i "${srcdir}/0002-fix-build-errors-mingw.patch"

  # Change to UTF-8 because patch can not. upstream #7034
  iconv -f UCS-2LE -t UTF-8 client/Windows/wfreerdp.rc  > client/Windows/wfreerdp.rc.mod
  sed -i '1s/^\xEF\xBB\xBF//' client/Windows/wfreerdp.rc.mod
  mv client/Windows/wfreerdp.rc.mod client/Windows/wfreerdp.rc

  # Duplicate macros
  patch -Np1 -i "${srcdir}/0003-remove-declspec-import-export.patch"

  # NTLM_AV_ID conflicts with MSV1_0_AVID
  patch -Np1 -i "${srcdir}/0004-remove-conflict-ntml-type.patch"

  # The variable name 'interface' assumed as keyword though it's C.
  patch -Np1 -i "${srcdir}/0005-rename-interface-variable.patch"

  # fix man page formatting:
  # https://bugs.archlinux.org/task/64814
  patch -Np1 -i "${srcdir}/0006-fix-manpage_formatting.patch"

  # https://github.com/FreeRDP/FreeRDP/pull/7048
  patch -Np1 -i "${srcdir}/0007-read-QuadPart-from-LARGE_INTEGER.patch"

  patch -Np1 -i "${srcdir}/0008-fix-build-clang-x64.patch"

  # https://github.com/FreeRDP/FreeRDP/commit/511444a6
  rm -f cmake/FindOpenSSL.cmake
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  CFLAGS+=" -Wno-format -Wno-int-conversion" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -Wno-dev \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_INSTALL_LIBDIR='lib' \
    -DCMAKE_BUILD_TYPE='None' \
    -DPROXY_PLUGINDIR='${MINGW_PREFIX}/lib/freerdp2/server/proxy/plugins' \
    -DWITH_CAIRO=ON \
    -DWITH_ICU=ON \
    -DWITH_ZLIB=ON \
    -DWITH_DSP_FFMPEG=ON \
    -DWITH_FFMPEG=ON \
    -DWITH_JPEG=ON \
    -DWITH_SERVER=ON \
    -DWITH_SWSCALE=ON \
    -DWITH_CHANNELS=ON \
    -DWITH_CLIENT_CHANNELS=ON \
    -DWITH_SERVER_CHANNELS=ON \
    -DCHANNEL_URBDRC_CLIENT=ON \
    "../${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .
  install -vDm0644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
